<template>
	<view>
		<head-navbar :back="true" class="headNavbar" :bgColor="'#FE5900'">
			<view>
				<text style="color: #FFFFFF;">{{ title }}</text>
			</view>
		</head-navbar>

		<view class="content" v-if="list.length > 0  && type < 2">
			<view class="list">
				<view class="item" v-for="(item,index) in list" :key="index">
					<view class="top">
						<view class="left">
							<text class="num">订单号：</text>
							<text>
				                {{item.orderNo}}
				            </text>
						</view>
						<view class="right" v-if="type == 0">
							<text style="padding-right: 20upx;">订单类型：{{item.orderTypeName}}</text>
							<text>{{item.statusName}}</text>
						</view>
						<!-- 买入 -->
						<view class="right" v-else-if="type == 1" @click="cancelOrder(item)">
							<text>取消</text>
						</view>
						<!-- 取消列表 2 -->
						<view class="right" v-else-if="type == 2">
							<text style="padding-right: 20upx;">取消预售</text>
							<text>已取消</text>
						</view>
						<!-- 取消列表 3 -->
						<view class="right" v-else-if="type == 3">
							<text style="padding-right: 20upx;">自用订单</text>
							<text>待发货</text>
						</view>
						<!-- 取消列表 4 -->
						<view class="right" v-else-if="type == 4" style="display: flex;align-items: center;">
							<text style="padding-right: 20upx;">预售订单</text>
							<view class="copy">复制</view>
						</view>
					</view>
					<view class="mid">
						<image :src="item.goodsSpecsImg" mode=""></image>
						<view class="intro">
							<view class="title">{{item.goodsName}}</view>
							<view class="size">
								<view>{{item.goodsSpecsName}}</view>
								<view>x{{item.quantity}}</view>
							</view>
						</view>
					</view>
					<view class="bot" v-if="type == 0 || type == 1">
						<text>实付款</text>
						<text class="money">￥{{item.paymentAmount + item.freight}}</text>
					</view>
					<view class="bot" v-else-if="type == 2">
						<text>返还：</text>
						<image src="../../../static/img/listChuangye.png" mode="" style="width: 44upx;height: 35upx;"></image>
						<text class="money">￥{{item.basePrice}}</text>
					</view>
					<view class="bot" v-else-if="type == 4 || type == 3" style="display: flex;align-items: center;justify-content: space-between;">
						<view class="money">￥{{item.basePrice}}</view>
						<view class="rightTXT">
							<text class="p-r-10">取消预售</text>
							<text class="p-r-10" @click="upSet">上架</text>
							<text class="p-r-10">申请自用</text>
							<view style="display: flex;align-items: center;">
								<image src="../../../static/img/shareIcon.png" mode="" style="width: 24upx;height: 28upx;"></image>
								<text style="color: #A9A9A9;">分享</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="content" v-else>
			<view class="list">
				<view class="items" v-for="(item,index) in list" :key="index">
					<view class="top">
						<view class="orderNo">预售订单号：{{item.orderNo || 110120114119}}</view>
						<view class="order">预售订单</view>
						<view class="status">复制</view>
					</view>
					<view class="middle">
						<image :src="item.goodsSpecsList[0].goodsSpecsImg || '../../../static/img/avtr.png'"></image>
						<view class="right">
							<view class="title">{{item.goodsName}}</view>
							<view class="content">
								<view class="spec">规格：{{item.goodsSpecsList[0].goodsSpecsName}}</view>
								<view class="num">x {{item.goodsSpecsList[0].stock}}</view>
							</view>
						</view>
					</view>
					<view class="bottom">
						<view class="priceBox">
							<view class="price">￥<text>{{item.goodsSpecsList[0].price || 0.00}}</text></view>
						</view>
						<view class="btns" v-if="type ==3">
							<view @click.stop="cancelBtn(item)">取消预售</view>
							<view @click.stop="Offshelf(item.goodsId)">上架</view>
							<view @click.stop="selectShow(item)">申请自用</view>
						</view>
						<view class="share">
							<image src="../../../static/img/icon_share.png"></image>
							<view class="text">分享</view>
						</view>
					</view>
				</view>
			</view>
		</view>	
		<u-loadmore v-if="type > 2" :status="status" bg-color="rgba(255,255,255,0)" margin-top='15' margin-bottom='15' @loadmore='laodmore' :loadText="loadText"/>
		<u-empty  text="列表空荡荡" mode="data" :show="list.length == 0 " margin-top="300" icon-size="200" font-size="40"></u-empty>
		<!-- 选择规格 S -->
		<u-popup v-model="seleteShow" mode="bottom" border-radius="20">
			<view class="selectBox">
				<view class="top">
					<image v-if="specSelected.length>0" :src="specSelected[0].goodsSpecsImg" mode="" class="goodsImg"></image>
					<view class="right">
						<view class="selected">
							价格：
							<text class="price" v-if="specSelected.length>0" >￥{{specSelected[0].price}}</text>
						</view>
						<text v-if="specSelected.length>0">库存 {{specSelected[0].stock}} 件</text>
						<view class="selected">
							规格：
							<text class="selected-text"  v-if="specSelected.length>0">
							{{specSelected[0].goodsSpecsName}}
						</text>
						</view>
					</view>
				</view>
				<view class="attr-list">
					<text>规格：</text>
					<view class="item-list">
						<text v-for="(childItem, index) in specList" :key="index" class="tit" :class="{selected: childItem.selected}"
						 @click="selectSpec(childIndex, childItem.goodsSpecsId)">
							{{childItem.goodsSpecsName}}
						</text>
					</view>
				</view>
		
				<view class="count">
					<text>数量</text>
					<view class="right" v-if="specSelected.length>0">
						<u-number-box v-model="num" :min="1"  :max='specSelected[0].stock'></u-number-box>
					</view>
				</view>
		
				<button class="btnSelect" @click="buy">确定</button>
			</view>
		</u-popup>
		<!-- 选择规格 E -->
	</view>
</template>

<script>
	import * as api from '@/api/user/index.js';
	import * as api2 from '@/api/order/index.js';
	import {cancelOrder,goodsUpdateStatus} from '@/api/order/index.js';
	import {mySaleList,getOrderList} from '@/api/order/index.js';
	export default {
		data() {
			return {
				status: 'loading',
				loadText: {
						loadmore: '获取数据错误,点击重新加载',
						loading: '加载中',
						nomore: '已经到底啦'
					},
				goodlist:[],
				seleteShow: false,
				type:0,
				title:'',
				show:true,
				requestData:{
					pageNum:1,
					pageSize:20,
					status:1
				},
				list:[],
				specSelected: [],
				specList: [],
				merchantHead: '',
				merchantName: '',
				goodsId: '',
				goodsName: '',
				pageNum:1,
				pageSize:20,
				myType:'',
				popupshow: false,
				payType: 1,
				num:1
			}
		},
		onLoad(option){
			var t = option.type;
				t = Number(t);
			this.type = t;
			this.requestData.status  = t;
			switch (t) {
				case 0:
					this.title = '我卖出的';
					this.requestData.status = '';
					break;
				case 1:
					this.title = '我买入的';
					this.requestData.status = '';
					this.requestData.orderType = 5;
					break;
				case 4:
					this.title = '取消列表';
					this.status2 = 5
					break;
				case 5:
					this.title = '自用列表';
					this.status2 = 1
					break;
				case 3:
					this.title = '已下架列表';
					this.status2 = 4
					break;
				default:
					break;
			}
			if(t == 0){
				this.getMySaleList();
			}else if(t == 1){
				this.getMybuyerList();
			}else{
				this.getMygoodsList();
			}
		},
		onReachBottom(){
			if(this.type > 2){
			if(this.status == 'nomore'){
				return
			}
			this.getMygoodsList()
			}
		},
		methods: {
			async getMygoodsList() {
				try{
					let parmas = {
						pageSize:this.pageSize,
						pageNum:this.pageNum,
						status:this.status2
					}
					const ret = await api.getMygoods(parmas);
					this.list = this.list.concat(ret.data.obj.list);
					if(ret.data.obj.list.length < this.pageSize){
						this.status = 'nomore'
					}
					this.pageNum++
				}catch(error){
					this.status = 'loadmore'
				}
			},
			async Offshelf(item) {
				try {
					let param = {
						goodsId: item,
						status: 1
					}
					const ret = await goodsUpdateStatus(param);
					this.list =[]
					this.pageNum = 1
					this.status = 'loading'
					this.getMygoodsList()
					uni.showToast({
						title:ret.data.msg,
						icon:'none'
					})
				} catch (error) {
			       this.list =[]
			       this.pageNum = 1
			       this.status = 'loading'
			       this.getMygoodsList()
			       uni.showToast({
			       	title:error.data.msg,
			       	icon:'none'
			       })
				}
			},
			async cancelBtn(item) {
				this.myType = 1
				this.specSelected = []
				this.specSelected.push(item.goodsSpecsList[0]);
				this.specList = item.goodsSpecsList
				this.$set(item.goodsSpecsList[0], 'selected', true);
				this.seleteShow = true;
			},
			selectShow(item) {
				this.goodsId = item.goodsId
				this.myType = 2
				this.specSelected = []
				this.specSelected.push(item.goodsSpecsList[0]);
				this.specSelected[0].merchantName = item.merchantName
				this.specSelected[0].merchantHead = item.merchantHead
				this.specSelected[0].goodsName = item.goodsName
				this.specList = item.goodsSpecsList
				for(var i =0;i<this.specList.length;i++){
					this.specList[0].merchantHead =  item.merchantHead
					this.specList[0].merchantName =  item.merchantName
					this.specList[0].goodsName =  item.goodsName
				}
				this.$set(item.goodsSpecsList[0], 'selected', true);
				this.seleteShow = true;
			},
			upSet(){

			},
			async cancelOrder(item) {
				this.myType = 1
				try{
					let param = {
						orderId:item.orderId,
						quantity:item.quantity
					};
					const ret = await cancelOrder(param);
					this.utils.toast(ret.data.msg);
				}catch(error){
					uni.showToast({
						title:error.data.msg,
						icon:'none'
					})
				}
			},
			//选择规格
			selectSpec(index, pid) {
				let list = this.specList;
				list.forEach(item => {
					this.$set(item, 'selected', false);
				})
				this.$set(list[index], 'selected', true);
				this.specSelected = [];
				this.specSelected.push(list[index]);
			},
			buy() {
				this.seleteShow = false;
				this.specSelected[0].num = this.num 
			
				let goodsList = this.specSelected
				if(this.myType == 2){
				try {
					uni.setStorageSync('saleData', goodsList);
				} catch (e) {
			
				}
				this.toPage(0, '/pages/my/preSale/orderPreSale/createOrder?goodsId='+this.goodsId);
				}else if(this.myType ==1){
						let params = {
							orderId: this.specSelected[0].orderId,
							quantity: this.num
						}
					api2.cancelOrder(params).then(res=>{
						this.list =[]
						this.pageNum = 1
						this.status = 'loading'
						this.getMygoodsList()
						uni.showToast({
							title:res.data.msg,
							icon:'none'
						})
					}).catch(err=>{
						this.list =[]
						this.pageNum = 1
						this.status = 'loading'
						this.getMygoodsList()
						uni.showToast({
							title:err.data.msg,
							icon:'none'
						})
					})
				}
			},
			async getMybuyerList(){
				try{
					let param = this.requestData;
					const ret = await getOrderList(param);
					this.list = ret.data.obj;
				}catch(error){
					uni.showToast({
						title:error.msg,
						icon:'none'
					})
				}
			},
			async getMySaleList(){
				try {
					let param = this.requestData;
					const ret = await mySaleList(param)
					this.list = ret.data.obj
				} catch(error) {
					uni.showToast({
						title:error.msg,
						icon:'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss">
@import './index.scss';
@import '../preSale/orderCom.scss';
</style>
